﻿#pragma once
#include<iostream>
#include <filesystem>
#include <fstream>
#include <stdint.h>
#include <memory>
#include <unordered_map>
#include <vector>
#include <base_type/bar_type.h>
#include<list>
#include<map>

#include "file_base.h"
#include<forward_list>

#pragma pack(push,4)
struct tdx_1d_t {
	uint32_t date;//高7bit必然是0
	uint32_t open;
	uint32_t high;
	uint32_t low;
	uint32_t close;
	float amount;
	uint32_t volume;
	uint32_t dump;
};
#pragma pack(pop)

#pragma pack(push,4)
struct tdx_1m_t {//
	//高内存地址放整数的高位，低内存地址放整数的低位,小端
	uint32_t date;
	float open;
	float high;
	float low;
	float close;
	float amount;
	uint32_t volume;
	uint32_t dump;
	uint32_t get_day() {
		uint32_t day = date & 0x0000ffff;//前2个字节放日期，低位
		uint32_t v = (day / 2048 + 2004)*10000;// year  前6bit用于表示年偏移
		v += day % 2048 ;//month+day
		//v += day % 2048 % 100;//day
		return v;
	}

};
#pragma pack(pop)




class TdxFileStore :public FileStoreBase {
public:
	TdxFileStore(std::string dir) :FileStoreBase(dir) {};
	//full path
	virtual std::filesystem::path get_filepath(Exchange exchange, std::string symbol, BarSize barsize) override;
	//path without filename
	std::filesystem::path get_path(Exchange exchangeid,  BarSize barsize);
	// return 32 staticlly
	virtual uint16_t row_size(BarSize barsize) override;
	//all file names under target path
	std::forward_list<std::string> get_symbols(Exchange exchange,BarSize bs);
	//get 000001.sh date list
	std::unordered_map<uint32_t, int32_t> get_trading_date(std::vector<char>* b);
	std::unordered_map<uint32_t, int32_t> get_trading_date(std::string symbol = "-");
	//iterate symbols
	std::filesystem::directory_iterator symbol_iterator(Exchange e,BarSize bs);
	
	//begin and end date for bar file
	std::pair<uint32_t, uint32_t> get_range(std::vector<char>* d);

	//读取整个file并解析为bar
	 std::vector<Bar> get_daybar(Exchange exchange, std::string symbol) ;
	 std::map<uint32_t, std::vector<MinuteBar>> get_minutebar(Exchange exchange, std::string symbol) ;
	
};
